/**
 *
 * @description:主容器控制器
 * @author:junyong.hong
 * @createTime:2017.10.30
 * @version:1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */
define([
    'app',

], function (app) {
    'use strict';
    app.controller('wrapper.controller', ['$rootScope', '$scope','$state','$location','$anchorScroll','$timeout',
        function ($rootScope,$scope, $state, $location, $anchorScroll, $timeout) {
            var wrapperVM = this;
            // console.log(project);

            wrapperVM.data = [{'name': '仪表盘', 'href': 'index.dashboard', 'position': {'x': '-1', 'y': '-1'}}];
            /**
             * 收广播
             * 接收（sideMenu.controller.js）的广播
             */
            $scope.$on('tabData',function (event, data) {
                // console.log(event,data);
                wrapperVM.data = data[0];//所有tab的数据
                wrapperVM.tabTitle = data[1];//当前打开打tab


                if(wrapperVM.data.length > wrapperVM.qsTabMax){
                    wrapperVM.scrollRight = true;

                    wrapperVM.initActiveTab();
                }

            });

            wrapperVM.initActiveTab = function () {
                var $ul = $(".oli-wrapper__content__tab__ul");

                var $liWidth = 10;
                var $ulMarginLeft = $ul[0].style.marginLeft;

                var $nowMarginLeft = parseFloat($ulMarginLeft.slice(0, $ulMarginLeft.length-1));
                $timeout(function () {
                    var $li = $(".oli-wrapper__content__tab__ul li");
                    var $liIndex = $(".oli-wrapper__content__tab__ul li.active").index();
                    if($nowMarginLeft + $liIndex * $liWidth < 0){
                        $ul.stop(true, false).animate({"margin-left": "0%"}, 500);
                        wrapperVM.scrollLeft = false;
                        wrapperVM.scrollRight = true;
                    } else if(($liIndex - 10 + 1) * $liWidth + $nowMarginLeft > 0){
                        $ul.stop(true, false).animate({"margin-left":  - ($li.length - wrapperVM.qsTabMax) * $liWidth + "%"}, 500);
                        wrapperVM.scrollRight = false;
                        wrapperVM.scrollLeft = true;
                    }
                }, 500);
            };

            /**
             * 切换tab
             * @param item 当前点击的item
             */
            wrapperVM.qs_on = function (item) {
                wrapperVM.tabTitle = item.name;

                $state.go(item.href);

                $rootScope.$broadcast('thisMenu',item);
            };

            /**
             * 关闭tab
             */
            wrapperVM.qs_off = function (index,item) {
                if (wrapperVM.data.length > 1 && item.href != 'index.dashboard') {
                    wrapperVM.tabs = del(index + 1, wrapperVM.data);
                }
            };


            // 按坐标删除元素
            function del(index, dataArray) {
                var len = dataArray.length;//当前打开tab的长度

                for (var i = 0; i < len; i++) {
                    if (i == index - 1) {
                        for (var j = i + 1; j < len; j++) {
                            // 当前索引值后的数据都向前移
                            dataArray[j - 1] = dataArray[j];
                        }
                        // 移完之后,自身长度减1
                        dataArray.length--;
                    }
                }
                return dataArray;
            }


            /**
             * 目录显示隐藏切换
             *
             **/
            // 页面加载默认为open
            wrapperVM.MenuMin = 'open';
            wrapperVM.MenuMinMark = '◀';
            wrapperVM.MenuMinMaxChangeFun = function(){
                // 隐藏的时候
                if(wrapperVM.MenuMin == 'close'){
                    wrapperVM.MenuMin = 'open';
                    wrapperVM.MenuMinMark = '◀';
                    return false;
                }
                // 显示的时候
                if(wrapperVM.MenuMin == 'open'){
                    wrapperVM.MenuMin = 'close';
                    wrapperVM.MenuMinMark = '▶';
                    return false;
                }
            };

            wrapperVM.scrollHorizontal = function ($event) {
                var $ul = $($event.target).nextAll("ul");
                if($ul.is(":animated")) return;     // :animated 选择所有动画
                var $liWidth = 10;                  //每个li占10%，这里固定
                var $ulMarginLeft = $ul[0].style.marginLeft;//获取到页面上oli-wrapper__content__tab__ul的margin-left

                var $nowMarginLeft;
                var $newMarginLeft;

                if($ulMarginLeft == ""){
                    $ulMarginLeft = "0%";
                }
                $nowMarginLeft = parseFloat($ulMarginLeft.slice(0, $ulMarginLeft.length-1));
                if($($event.target).hasClass("tab__scroller__left")){
                    $newMarginLeft = $nowMarginLeft + $liWidth;
                    wrapperVM.scrollRight = true;
                } else {
                    $newMarginLeft = $nowMarginLeft - $liWidth;
                    wrapperVM.scrollLeft = true;
                }


                if(Math.abs($newMarginLeft) < 0.001){
                    wrapperVM.scrollLeft = false;
                    $newMarginLeft = 0;
                } else if((wrapperVM.data.length-10)*$liWidth + $newMarginLeft < 0.001){
                    wrapperVM.scrollRight = false;
                }

                $ul.stop(true,false).animate({"margin-left" : $newMarginLeft+"%"}, 500);

            };

            function init() {
                wrapperVM.qsTabMax = 10;        //tab最多打开10个
                wrapperVM.scrollLeft = false;   //左滑动按钮 -> 隐藏
                wrapperVM.scrollRight = false;  //右滑动按钮 -> 隐藏

            }

            init();


    }])
});